import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import dts from "vite-plugin-dts";
import path from "path";
import postcss from "rollup-plugin-postcss";
import Components from "unplugin-vue-components/vite";

export default defineConfig({
  plugins: [
    vue(),
    Components({
      dirs: ["src/components"],
      extensions: ["vue"],
      dts: true,
    }),
    dts({ insertTypesEntry: true }),
    postcss({
      extract: true, // 自动提取 CSS 到 dist/index.css
      modules: false,
      use: ["sass"],
      // 禁止自动从 .vue 文件中提取样式
      namedExports(name: string) {
        return name.replace(/\.[^/.]+$/, ""); // 去掉 .css 后缀
      },
    }),
  ],
  resolve: {
    alias: {
      "@theme": path.resolve(__dirname, "./theme-chalk/src"),
      "@": path.resolve(__dirname, "./src"),
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "@theme/_variables" as *;`,
      },
    },
  },
  build: {
    lib: {
      entry: "./src/components/index.ts",
      name: "SnobyComponents",
      fileName: (format) => `index.${format === "es" ? "mjs" : "cjs"}`,
    },
    minify: false,
    rollupOptions: {
      external: ["vue", "element-plus"],
      output: {
        exports: "named",
        globals: {
          vue: "Vue",
          "element-plus": "ElementPlus",
        },
      },
    },
  },
});
